//
//  homeHeadView.m
//  有材网
//
//  Created by 肖扬 on 16/2/1.
//  Copyright © 2016年 肖扬. All rights reserved.
//

#import "homeHeadView.h"
#import "SUNScrollImage.h"
#import "homeModel.h"
#import "DCPicScrollView.h"
#import "TitleImageButton.h"
@implementation homeHeadView
@synthesize scroll,images;
-(id)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.images = [NSMutableArray array];
        self.scroll = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 192)];
        self.scroll.backgroundColor = [UIColor clearColor];
        [self addSubview:self.scroll];
    }
    return self;
}
#define WS(weakSelf)  __weak __typeof(&*self)weakSelf = self;
-(void)setHeadView:(NSMutableArray *)imageArray{
    if ([imageArray count]!=0) {
        self.images = imageArray;
        if ([imageArray count]==1) {
            homeModelBanner *banner = [imageArray objectAtIndex:0];
            SUNScrollImage * temp = [[SUNScrollImage alloc]initWithFrame:CGRectMake(0, 0, SWIDTH, 180)];
            [temp.imageButton addTarget:self action:@selector(ImgNews:) forControlEvents:UIControlEventTouchUpInside];
            temp.imageButton.tag = 30;
            if (![Unity isBlankString:banner.image_url]) {
                [temp.imageButton setImageURL:[NSURL URLWithString:banner.image_url]];
            }
            temp.backgroundColor = [UIColor clearColor];
            temp.backgroundColor = [UIColor clearColor];
            [self.scroll addSubview:temp];
        }else{
            WS(wself)
            NSMutableArray *arr = [NSMutableArray array];
            for (int i = 0; i <[imageArray count]; i++) {
                homeModelBanner *banner = [imageArray objectAtIndex:i];
                [arr addObject:banner.image_url];
            }
            DCPicScrollView  *picView = [[DCPicScrollView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, 192) WithImageNames:arr];
            
            //    picView.titleData = arr1;
            picView.placeImage = [UIImage imageNamed:@"缺省图"];
            [picView setImageViewDidTapAtIndex:^(NSInteger index) {
                printf("你点到我了😳index:%zd\n",index);
                [wself selectImg:index];
            }];
            picView.AutoScrollDelay = 1.0f;
            [self addSubview:picView];
        }
    }
    
    UIView *tempView =[[UIView alloc]initWithFrame:CGRectMake(0, 192, SWIDTH, 88)];
    tempView.backgroundColor = [UIColor whiteColor];
    [self addSubview:tempView];
    
    NSMutableArray *arr = [NSMutableArray arrayWithObjects:@"我要采购",@"我要供货",@"今日特价",@"直销商城",@"快速发布", nil];
    NSMutableArray *imgArr = [NSMutableArray arrayWithObjects:@"我要采购-拷贝",@"我要供货",@"今日特价",@"直销商城",@"快速发布", nil];
    int w = SWIDTH/5;
    int h = 75;
    for (int i = 0; i < [arr count]; i++) {
        TitleImageButton*btn=[TitleImageButton createBtnFrame:CGRectMake(w*i, 10, w, h) title:arr[i] imageName:imgArr[i] selecetdImage:@"" titleColor:[UIColor blackColor] target:self action:@selector(clickThisBtn:) style:TitleImageButtonDistributeStyleImageTopAndBottom tag:100+i];
        btn.textAlignment=NSTextAlignmentCenter;
        btn.imageMode=UIViewContentModeTop;
        btn.imageRatio=0.6;
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [tempView addSubview:btn];
    }
    
    UIView *tempView1 =[[UIView alloc]initWithFrame:CGRectMake(0, 280, SWIDTH, 110)];
    tempView1.backgroundColor = [UIColor clearColor];
    [self addSubview:tempView1];
    
    UIView *tempView2 =[[UIView alloc]initWithFrame:CGRectMake(0, 10, SWIDTH, 90)];
    tempView2.backgroundColor = [UIColor whiteColor];
    [tempView1 addSubview:tempView2];

    int y = (90 - 71)/2;
    int bw = 94;
    int bh = 71;
    for (int i = 0; i < 3; i++) {
        
        UIButton *button = [self createButtonFrame:CGRectMake(SWIDTH/3*i, 0, SWIDTH/3, 90) title:@"" viewController:self selector:@selector(home3Clcik:) tag:i+10];
        UIView *view = nil;
        if (i == 0) {
            button.frame = CGRectMake(10, y, bw, bh);
            [button setBackgroundImage:[UIImage imageNamed:@"板材背景"] forState:0];
            view= [self returnView:button.frame ImageView:25 height:25 image:@"首页-板材" title:@"板材" tag:i+10];
        }
        if (i == 1) {
            button.frame = CGRectMake(SWIDTH/2 - 47, y, bw, bh);
            [button setBackgroundImage:[UIImage imageNamed:@"门板背景"] forState:0];
            view = [self returnView:button.frame ImageView:15 height:26 image:@"首页-门板" title:@"门板" tag:i+10];
        }
        if (i == 2) {
            button.frame = CGRectMake(SWIDTH - 104, y, bw, bh);
            [button setBackgroundImage:[UIImage imageNamed:@"装饰纸背景"] forState:0];
            view = [self returnView:button.frame ImageView:26 height:26 image:@"首页-装饰纸" title:@"装饰纸" tag:i+10];
        }
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapClick:)];
        [view addGestureRecognizer:tap];
        [button addSubview:view];
        [tempView2 addSubview:button];
    }

}
-(UIView *)returnView:(CGRect)frame
            ImageView:(int)width
               height:(int)height
                image:(NSString *)image
                title:(NSString *)title
                  tag:(int)tag{
    UIView *view = [self createViewFrame:CGRectMake(0, 0, 94, 71) bgColor:[UIColor clearColor] tag:0];
    view.tag = tag;
    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake((94 - width)/2, 45-height, width, height)];
    imageV.image = [UIImage imageNamed:image];
    [imageV setBackgroundColor:[UIColor clearColor]];
    [view addSubview:imageV];
    UILabel *lab = [[UILabel alloc]initWithFrame:CGRectMake(0, 52, 94, 11)];
    lab.textColor = [UIColor whiteColor];
    lab.backgroundColor = [UIColor clearColor];
    lab.font = [UIFont systemFontOfSize:11];
    lab.text = title;
    lab.textAlignment = NSTextAlignmentCenter;
    [view addSubview:lab];
    return view;
}

//创建View tag值为五位数
- (UIView *)createViewFrame:(CGRect)frame
                    bgColor:(UIColor *)color
                        tag:(NSInteger)tag{
    UIView *tempView = [[UIView alloc]initWithFrame:frame];
    [tempView setBackgroundColor:color];
    [tempView setTag:tag];
    return tempView;
}
//创建UIButton tag值为四位数
- (UIButton *)createButtonFrame:(CGRect)frame
                          title:(NSString *)title
                 viewController:(id)viewController
                       selector:(SEL)selector
                            tag:(NSInteger)tag{
    UIButton *tempButton = [UIButton buttonWithType:0];
    [tempButton setFrame:frame];
    [tempButton setTag:tag];
    [tempButton setBackgroundColor:[UIColor clearColor]];
    [tempButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [tempButton setTitle:title forState:UIControlStateNormal];
    [tempButton addTarget:viewController action:selector forControlEvents:UIControlEventTouchUpInside];
    return tempButton;
}
-(void)clickThisBtn:(TitleImageButton *)sender{
    
}
-(void)selectImg:(NSInteger)index{
    homeModelBanner *banner = [images objectAtIndex:index];
    if (![Unity isBlankString:banner.link_url]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:banner.link_url]];
    }
}
-(void)ImgNews:(EGOImageButton *)sender{
    homeModelBanner *banner = [images objectAtIndex:sender.tag - 30];
    if (![Unity isBlankString:banner.link_url]) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:banner.link_url]];
    }
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
